<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Sign;
use App\Models\UserInfo;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Table;

class SignController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Sign(['getUserInfo']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('getUserInfo.nickName', '用户昵称');
            $grid->column('sum_score', '总积分');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch('getUserInfo.nickName')->placeholder('请输入用户昵称')->auto(false);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            //详情模态框
            $grid->column('详情')->display('详情')->modal(function ($modal) {
                // 设置弹窗标题
                $modal->title('积分领取明细');
                // 1140px
                $modal->xl();
                $id = $modal->row->user_id;
                //关联查询积分明细信息
                $list = (new \App\Models\Integral())->where('user_id', $id)->get(['score', 'content', 'created_at'])->toArray();
                $userInfo = (new UserInfo())->where('id', $id)->get(['nickName', 'tel'])->toArray();
                if ($list != null && $userInfo != null) ;
                //循环遍历并合并数组
                foreach ($list as $k => $v) {
                    $list[$k] = array_merge($userInfo[0], $list[$k]);
                }
                $titles = [
                    '用户昵称',
                    '用户手机号',
                    '积分变化',
                    '积分明细',
                    '变动时间',
                ];
                return Table::make($titles, $list);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Sign(), function (Show $show) {
            $show->field('id');
            $show->field('sum_score');
            $show->field('sign_day');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Sign(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('sum_score');
            $form->text('sign_day');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
